﻿using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using DevComponents.DotNetBar;
using System.Windows.Forms;

namespace Shine.Framework
{
    public class DockManagerClass : IDockManager
    {
        private DotNetBarManager m_DotNetBarManager = null;
        private Form m_MainForm = null;
        public object DockManager
        {
            set
            {
                this.m_DotNetBarManager = value as DotNetBarManager;
            }
        }
        public Form MainForm
        {
            set
            {
                m_MainForm = value;
            }
        }

        public DockManagerClass()
        {

        }

        public void ShowOverviewMap()
        {
            if (m_MainForm == null) return;
            foreach(Control c in m_MainForm.Controls)
            {
                if (c.Text == "鹰眼图")
                {
                    c.Visible = !c.Visible;

                    break;
                }
            }
        }
        public bool AddDockPanel(string text, Control control, bool isReplace, esriDockFlags sides)
        {
            Bar bar = GetDockBar(text);
            if (bar != null && isReplace)
            {
                ((DockContainerItem)bar.Items[text]).Control = control;
                return true;
            }
            else if (bar == null)
            {
                DockSite docksite = null;
                switch (sides)
                {
                    case esriDockFlags.esriDockLeft:
                        docksite = m_DotNetBarManager.LeftDockSite;
                        break;
                    case esriDockFlags.esriDockBottom:
                        docksite = m_DotNetBarManager.BottomDockSite;
                        break;
                    case esriDockFlags.esriDockRight:
                        docksite = m_DotNetBarManager.RightDockSite;
                        break;
                    case esriDockFlags.esriDockFloat:
                        docksite = m_DotNetBarManager.FillDockSite;
                        break;
                    default:
                        throw new NotImplementedException();
                }
                if (docksite.Controls.Count == 0)
                {
                    bar = CreateDockBar(text);
                }
                else
                {
                    if (docksite.Controls[0] is Bar && !(((Bar)docksite.Controls[0]).AutoHide))
                    {
                        bar = docksite.Controls[0] as Bar;
                    }
                    else
                    {
                        bar = CreateDockBar(text);
                    }
                }
                DockContainerItem dockItem = new DockContainerItem(text, text);
                dockItem.Control = control;
                bar.Items.Add(dockItem);
                bar.Visible = true;
                bar.SelectedDockContainerItem = dockItem;
                if (!docksite.Controls.Contains(bar))
                    docksite.GetDocumentUIManager().Dock(bar);
                // dockItem.Width = Width / 4;
                bar.RecalcLayout();
                return true;
            }
            return true;
        }

        public Control GetControlFromDockPanel(string itemText)
        {
            //Bar bar = GetDockBar(itemText);
            foreach (Bar bar in m_DotNetBarManager.Bars)
            {
                //if (bar.Text.Equals(itemText))
                //{
                //    return bar;
                //}
                //else
                //{
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Text.Equals(itemText))
                        {
                            DockContainerItem item = bar.Items[i] as DockContainerItem;
                            if (item != null)
                            {
                                PanelDockContainer panel = item.Control as PanelDockContainer;
                                if (panel != null && panel.Controls.Count>0)
                                {
                                   return  panel.Controls[0];
                                }
                                
                            }
                        }
                    }
                //}
            }
            return null;
        }

        public void ActivateDockPanel(string itemText)
        {
            foreach (Bar bar in m_DotNetBarManager.Bars)
            {
                if (bar.Items.Contains(itemText))
                {
                    DockContainerItem item = bar.Items[itemText] as DockContainerItem;
                    bar.SelectedDockContainerItem = item;                  
                    bar.RecalcLayout();
                    bar.Visible = true;                  
                }
                else
                {
                    for (int i = 0; i < bar.Items.Count; i++)
                    {
                        if (bar.Items[i].Text.Equals(itemText))
                        {
                            DockContainerItem item = bar.Items[i] as DockContainerItem;
                            item.Visible = true;
                            bar.SelectedDockContainerItem = item;                                                                          
                            bar.Visible = true;                         
                            bar.RecalcLayout();                           
                        }
                    }
                }
            }
        }

        public void RemoveDockPanel(string itemText)
        {
            Bar bar = GetDockBar(itemText);
            if (bar == null)
            {
                return;
            }
            
            bar.Items.Remove(itemText);
            bar.RecalcLayout();
        }

        private Bar CreateDockBar(string text)
        {
            Bar leftBar = new Bar(text);
            leftBar.CanDockTop = false;
            leftBar.CanUndock = false;
            leftBar.SaveLayoutChanges = false;
            leftBar.Name = text;
            leftBar.LayoutType = eLayoutType.DockContainer;
            leftBar.Stretch = true;
            leftBar.AutoHideAnimationTime = 100;
            leftBar.GrabHandleStyle = eGrabHandleStyle.Caption;
            m_DotNetBarManager.Bars.Add(leftBar);
            return leftBar;
        }

        private Bar GetDockBar(string text)
        {
            foreach (Bar bar in m_DotNetBarManager.Bars)
            {
                if (bar.Items.Contains(text))
                {
                    return bar;
                }
            }
            return null;
        }

        public bool AddTabItem(string text, Control control)
        {
            foreach (Bar bar in m_DotNetBarManager.Bars)
            {
                for (int i = 0; i < bar.Items.Count; i++)
                {
                    if (bar.Items[i] is DockContainerItem)
                    {
                        DockContainerItem pDockContainerItem = (DockContainerItem)bar.Items[i];
                        if (pDockContainerItem.Control is PanelDockContainer)
                        {
                            PanelDockContainer pPanelDockContainer = (PanelDockContainer)pDockContainerItem.Control;
                            for (int j = 0; j < pDockContainerItem.Control.Controls.Count; j++)
                            {
                                if (pPanelDockContainer.Controls[j] is DevComponents.DotNetBar.TabControl)
                                {
                                    DevComponents.DotNetBar.TabControl pTabControl = (DevComponents.DotNetBar.TabControl)pPanelDockContainer.Controls[j];
                                    bool flag=false;
                                    for (int k = 0; k < pTabControl.Tabs.Count; k++)
                                    {
                                        if (pTabControl.Tabs[k].Text == text)
                                            flag = true;
                                    }
                                    #region 增加三维视图
                                    if (!flag)
                                    {
                                        TabItem sceneView = new DevComponents.DotNetBar.TabItem();
                                        TabControlPanel pTabControlPanel = new DevComponents.DotNetBar.TabControlPanel();
                                        pTabControl.Tabs.Add(sceneView);
                                        pTabControl.Controls.Add(pTabControlPanel);
                                        sceneView.AttachedControl = pTabControlPanel;
                                        sceneView.Name = control.Name;
                                        sceneView.Text = text;

                                        // 
                                        // pTabControlPanel
                                        // 
                                        control.Dock = DockStyle.Fill;
                                        pTabControlPanel.Controls.Add(control);
                                        pTabControlPanel.Dock = System.Windows.Forms.DockStyle.Fill;
                                        pTabControlPanel.Location = new System.Drawing.Point(0, 25);
                                        pTabControlPanel.Name = control.Name + "TabControlPanel";
                                        pTabControlPanel.Padding = new System.Windows.Forms.Padding(1);
                                        pTabControlPanel.Size = new System.Drawing.Size(496, 264);
                                        pTabControlPanel.Style.BackColor1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(242)))), ((int)(((byte)(232)))));
                                        pTabControlPanel.Style.BackColor2.Color = System.Drawing.Color.White;
                                        pTabControlPanel.Style.Border = DevComponents.DotNetBar.eBorderType.SingleLine;
                                        pTabControlPanel.Style.BorderColor.Color = System.Drawing.Color.FromArgb(((int)(((byte)(172)))), ((int)(((byte)(168)))), ((int)(((byte)(153)))));
                                        pTabControlPanel.Style.BorderSide = ((DevComponents.DotNetBar.eBorderSide)(((DevComponents.DotNetBar.eBorderSide.Left | DevComponents.DotNetBar.eBorderSide.Right)
                                                    | DevComponents.DotNetBar.eBorderSide.Bottom)));
                                        pTabControlPanel.Style.GradientAngle = 90;
                                        pTabControlPanel.TabIndex = 3;
                                        pTabControlPanel.TabItem = sceneView;

                                        pTabControl.Refresh();
                                        return true;
                                    }
                                    #endregion
                                }
                            }
                        }

                    }
                }
            }
            return false;
        }

        public void ActivateTabItem(string itemText)
        {
            foreach (Bar bar in m_DotNetBarManager.Bars)
            {
                for (int i = 0; i < bar.Items.Count; i++)
                {
                    if (bar.Items[i] is DockContainerItem)
                    {
                        DockContainerItem pDockContainerItem = (DockContainerItem)bar.Items[i];
                        if (pDockContainerItem.Control is PanelDockContainer)
                        {
                            PanelDockContainer pPanelDockContainer = (PanelDockContainer)pDockContainerItem.Control;
                            for (int j = 0; j < pDockContainerItem.Control.Controls.Count; j++)
                            {
                                if (pPanelDockContainer.Controls[j] is DevComponents.DotNetBar.TabControl)
                                {
                                    DevComponents.DotNetBar.TabControl pTabControl = (DevComponents.DotNetBar.TabControl)pPanelDockContainer.Controls[j];
                                    bool flag = false;
                                    for (int k = 0; k < pTabControl.Tabs.Count; k++)
                                    {
                                        if (pTabControl.Tabs[k].Text == "三维视图")
                                        {
                                            pTabControl.SelectedTab = pTabControl.Tabs[k];
                                        }
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }
    }
}


//TabItem sceneView = new TabItem();
//control.Dock = DockStyle.Fill;
//sceneView.Name = control.Name+"0";
//sceneView.Text = "三维视图";

//TabControlPanel pTabControlPanel = new TabControlPanel();
//pTabControlPanel.Dock = System.Windows.Forms.DockStyle.Fill;
//pTabControlPanel.Location = new System.Drawing.Point(0, 25);
//pTabControlPanel.Name = "pTabControlPanel";
//pTabControlPanel.Padding = new System.Windows.Forms.Padding(1);
//pTabControlPanel.Size = new System.Drawing.Size(496, 264);
//pTabControlPanel.Style.BackColor1.Color = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(242)))), ((int)(((byte)(232)))));
//pTabControlPanel.Style.BackColor2.Color = System.Drawing.Color.White;
//pTabControlPanel.Style.Border = DevComponents.DotNetBar.eBorderType.SingleLine;
//pTabControlPanel.Style.BorderColor.Color = System.Drawing.Color.FromArgb(((int)(((byte)(172)))), ((int)(((byte)(168)))), ((int)(((byte)(153)))));
//pTabControlPanel.Style.BorderSide = ((DevComponents.DotNetBar.eBorderSide)(((DevComponents.DotNetBar.eBorderSide.Left | DevComponents.DotNetBar.eBorderSide.Right)
//            | DevComponents.DotNetBar.eBorderSide.Bottom)));
//pTabControlPanel.Style.GradientAngle = 90;
//pTabControlPanel.TabIndex = 3;
//pTabControlPanel.TabItem = sceneView;


//pTabControl.Controls.Add(pTabControlPanel);
//pTabControl.Tabs.Add(sceneView);

//sceneView.AttachedControl = pTabControlPanel;
